package com.nicklayton.betterhabits;

import com.nicklayton.betterhabits.R;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

public class ConfigurationActivity extends Activity
{
	
	private int p_iAppWidgetID;

	@Override
	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);
		
		setResult(RESULT_CANCELED);
		
		setContentView(R.layout.configurationlayout);
		
		findViewById(R.id.btnMonthly).setOnClickListener(p_oMonthlyOnClickListener);
		findViewById(R.id.btnWeekly).setOnClickListener(p_oWeeklyOnClickListener);
		findViewById(R.id.btnDaily).setOnClickListener(p_oDailyOnClickListener);
		
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null)
		{
			p_iAppWidgetID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		}
		
		// If they gave us an intent without the widget id, just bail.
		if (p_iAppWidgetID == AppWidgetManager.INVALID_APPWIDGET_ID)
		{
			finish();
		}
	}
	
	View.OnClickListener p_oMonthlyOnClickListener = new View.OnClickListener()
	{
		public void onClick(View v)
		{
			final Context context = ConfigurationActivity.this;
			
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
			
			RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.monthlylayout);
			appWidgetManager.updateAppWidget(p_iAppWidgetID, views);

			Intent resultValue = new Intent();
			resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, p_iAppWidgetID);
			setResult(RESULT_OK, resultValue);
			finish();
		}
	};
	
	View.OnClickListener p_oWeeklyOnClickListener = new View.OnClickListener()
	{
		public void onClick(View v)
		{
			final Context context = ConfigurationActivity.this;
			
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
			
			RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.weeklylayout);

			appWidgetManager.getAppWidgetInfo(p_iAppWidgetID).minHeight = 40;
			
			appWidgetManager.updateAppWidget(p_iAppWidgetID, views);

			appWidgetManager.getAppWidgetInfo(p_iAppWidgetID).minHeight = 40;
			
			Intent resultValue = new Intent();
			resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, p_iAppWidgetID);
			setResult(RESULT_OK, resultValue);
			finish();
		}
	};
	
	View.OnClickListener p_oDailyOnClickListener = new View.OnClickListener()
	{
		public void onClick(View v)
		{
			final Context context = ConfigurationActivity.this;
			
			Toast.makeText(context, "Daily widget has not been implemented.", Toast.LENGTH_SHORT).show();
		}
	};
}
